package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.getTimer;
	
	[SWF(width=800, height=600, backgroundColor="#0000FF")]
	public class main extends Sprite
	{
		public var loader:Loader;
		public var RED:BitmapData;
		public var GREEN:BitmapData;
		public var BLUE:BitmapData;
		public var ALPHA:BitmapData;
		
		public var bmd:BitmapData;
		public var bmp:Bitmap;
		
		public var count:int=0;
		
		public function main()
		{
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			loader.load(new URLRequest("../Impala0018.RED.png"));
		}
		public function onComplete(e:Event):void
		{
			count++;
			if(count == 1)
			{
				RED = Bitmap(loader.content).bitmapData;
				loader.load(new URLRequest("../Impala0018.GREEN.png"));
			}
			else if(count == 2)
			{
				GREEN = Bitmap(loader.content).bitmapData;
				loader.load(new URLRequest("../Impala0018.BLUE.png"));
			}
			else if(count == 3)
			{
				BLUE = Bitmap(loader.content).bitmapData;
				loader.load(new URLRequest("../Impala0018.ALPHA.png"));
			}
			else if(count == 4)
			{
				ALPHA = Bitmap(loader.content).bitmapData;
				startMerge();
			}
		}
		
		public function startMerge():void
		{
			var r:Vector.<uint> = RED.getVector(RED.rect);
			var g:Vector.<uint> = GREEN.getVector(GREEN.rect);
			var b:Vector.<uint> = BLUE.getVector(BLUE.rect);
			var a:Vector.<uint> = ALPHA.getVector(ALPHA.rect);
			
			var vect:Vector.<uint> = new Vector.<uint>(r.length,true);
			
			var i:int = 0;
			var l:int = r.length;
			for(i; i<l; i++)
			{
				vect[i] = ((a[i]&0xFF)<<24) | ((r[i]&0xFF)<<16) | ((g[i]&0xFF)<<8) | (b[i]&0xFF);
			}
			bmd = new BitmapData(RED.width, RED.height, true, 0);
			bmd.setVector(RED.rect, vect);
			bmp = new Bitmap(bmd);
			addChild(bmp);
		}
	}
}

















